import {getPca} from '../services/api';

export default {
  namespace: 'pca',

  state: {
    list: [],
    provinceList: [],
    cityList: [],
    selectedPcaNameList: [],
    selectedPcaNode: {
      id: 0,
      name: '全国',
    },
    pickerRange: [],
  },

  effects: {
    * getPca4Add(_, {call, put}) {
      const result = yield call(getPca);
      /*if (result && result.status === 0) {
        const provinces = result.result[0];
        const citys = result.result[1];
        const countys = result.result[2];

        let list = [];
        provinces.forEach(item => {
          let city = [];
          citys.forEach(item1 => {
            if (item.id.substr(0, 2) === item1.id.substr(0, 2)) {
              let county = [];
              countys.forEach(item2 => {
                if (item1.id.substr(0, 4) === item2.id.substr(0, 4)) {
                  county.push({
                    key: item2.id,
                    name: item2.fullname,
                  });
                }
              });

              city.push({
                key: item1.id,
                name: item1.fullname,
                children: county,
              });
            }
          });

          list.push({
            key: item.id,
            name: item.fullname,
            children: city,
          });
        });

        yield put({
          type: 'save',
          payload: list,
        });
      }*/
      const pickerRange = [[], [], []];
      result.forEach(item => {
        pickerRange[0].push({
          key: item.code,
          name: item.name,
        })
      });
      result[0].children.forEach(item => {
        pickerRange[1].push({
          key: item.code,
          name: item.name,
        })
      });
      result[0].children[0].children.forEach(item => {
        pickerRange[2].push({
          key: item.code,
          name: item.name,
        })
      });
      yield put({
        type: 'save',
        payload: {
          list: result,
          pickerRange,
          selectedPca: {
            id: pickerRange[2][0].key,
            name: pickerRange[2][0].name,
          }
        },
      });
    },

    * getPca4Query(_, {call, put}) {
      const result = yield call(getPca);

      const provinceList = [];
      const cityList = [];
      result.forEach(item => {
        provinceList.push({
          key: item.code,
          name: item.name,
        })
      });
      result[0].children.forEach(item => {
        cityList.push({
          key: item.code,
          name: item.name,
        })
      });

      yield put({
        type: 'save',
        payload: {
          list: result,
          provinceList,
          cityList,
        },
      });
    },

    * getCityList({payload}, {call, put}) {
      // TODO result => state.list
      const result = yield call(getPca);

      const cityList = [];
      result[payload].children.forEach(item => {
        cityList.push({
          key: item.code,
          name: item.name,
        })
      });

      yield put({
        type: 'save',
        payload: {
          cityList,
        },
      });
    },
  },

  reducers: {
    save(state, {payload}) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
